﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class SendTaskRequestCommand extends Command {

        public var event:TaskRequestEvent;
        public var missions:MissionModel;
        private var _taskAction:String;
        private var _taskIds:String;
        private var _itemType:String;
        private var _numActions:int;

        override public function execute():void{
            var _local4:RequestVO;
            var _local1:Object = this.event.info;
            this._taskAction = _local1.taskType;
            this._itemType = this.getItemType(_local1.itemType);
            this._taskIds = this.missions.getAffectedTaskIds(this._taskAction, this._itemType);
            this._numActions = (_local1.numActions) ? _local1.numActions : 1;
            var _local2:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onResult);
            _local2.errorHandler = _local1.onError;
            var _local3:Array = this.event.requests;
            for each (_local4 in _local3) {
                if (this._taskIds){
                    _local4.addParam(ProtocolConstants.TASK_IDS, this._taskIds);
                };
                _local2.addRequestVO(_local4);
            };
            dispatch(_local2);
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Function;
            var _local3:int;
            if (_arg1.success){
                if (this._taskIds){
                    _local3 = 0;
                    while (_local3 < this._numActions) {
                        this.missions.incrementAffectedTask(this._taskAction, this._itemType);
                        _local3++;
                    };
                };
                _local2 = (this.event.info.onResult as Function);
                if (_local2 != null){
                    _local2(_arg1);
                };
            };
        }
        private function getItemType(_arg1:String):String{
            var _local2:Array;
            switch (_arg1){
                case "RewardLove":
                case "RewardFire":
                case "RewardWater":
                case "RewardAir":
                case "RewardStar":
                case "RewardIce":
                case GameConstants.LOVE:
                case GameConstants.FIRE:
                case GameConstants.WATER:
                case GameConstants.AIR:
                case GameConstants.STAR:
                case GameConstants.ICE:
                    _local2 = this.missions.getTasksWithActionAndType(this._taskAction, GameConstants.ANY_GEMS);
                    if (_local2.length){
                        _arg1 = GameConstants.ANY_GEMS;
                    };
                    break;
            };
            return (_arg1);
        }

    }
}//package net.wooga.pets.controllers 
